﻿using System;
using NUnit.Framework;
using UseCaseManager.BO;
using UseCaseManager.Logic;
using UseCaseManager.Test.BO;

namespace UseCaseManager.Test.Logic
{
    [TestFixture]
    public class TestUseCaseManagerRegistry
    {
        [TestFixtureSetUp]
        public virtual void TestFixtureSetup()
        {
            //Clear and Load Class Defs
            TestUtils.TestFixtureSetup();
        }

        [SetUp]
        public virtual void SetupTest()
        {
            //-------------------------------------------------------------------------------------
            // You can choose here whether to run against a database or whether to use an in-memory
            // database for all the tests, which runs quickly. It doesn't however check that
            // your database has the correct structure, which is partly the purpose of these tests.
            // The generated tests do already use an in-memory database where possible.
            // In your custom tests, you can set them to use an in-memory database by copying the
            // line to the first line of your test.
            //-------------------------------------------------------------------------------------
            //BORegistry.DataAccessor = new DataAccessorDB();
            //Sets up In memory, clears business object manager
            TestUtils.TestSetup();
        }

        [Test]
        public void Test_GetsProject()
        {
            //---------------Set up test -----------------------
            Project expectedProject = TestUtilsProject.CreateSavedProject();
            //---------------Assert pre conditions -------------
            //---------------Execute Test ----------------------
            Project currentProject = UseCaseManagerRegistry.CurrentProject;
            //---------------Test Result -----------------------
            Assert.AreEqual(expectedProject, currentProject);
        }

        [Test]
        public void Test_GetsProject_IfNull_ThrowsException()
        {
            //---------------Set up test -----------------------
            //---------------Assert pre conditions -------------
            //---------------Execute Test ----------------------
            try
            {
                Project currentProject = UseCaseManagerRegistry.CurrentProject;
                Assert.Fail("Should throw exception when no project to be loaded.");
            }
            //---------------Test Result -----------------------
            catch (Exception ex)
            {
                StringAssert.Contains("No project has been loaded.", ex.Message);
            }
        }

        [Test]
        public void Test_GetsProject_IfMoreThanOneProject_ThrowsException()
        {
            //---------------Set up test -----------------------
            Project project = TestUtilsProject.CreateSavedProject();
            Project project2 = TestUtilsProject.CreateSavedProject();
            //---------------Assert pre conditions -------------
            //---------------Execute Test ----------------------
            try
            {
                Project currentProject = UseCaseManagerRegistry.CurrentProject;
                Assert.Fail("Should throw exception when more than one project is loaded.");
            }
            //---------------Test Result -----------------------
            catch (NotSupportedException ex)
            {
                StringAssert.Contains("Use Case Manager does not currently support more than 1 "
                                        + "project in the database.", ex.Message);
            }
        }

        [Test]
        public void Test_GetDrops_CurrentProject()
        {
            //---------------Set up test -----------------------
            Drop drop;
            Drop drop2;
            Drop drop3;
            Release release = TestUtilsRelease.CreateUnsavedRelease3DropsOrderdByEndDate(out drop, out drop2, out drop3);
            release.Save();
            const int expectedNumberOfDrops = 3;
            //---------------Assert pre conditions -------------
            //---------------Execute Test ----------------------
            //---------------Test Result -----------------------
            Assert.AreEqual(expectedNumberOfDrops, UseCaseManagerRegistry.Drops.Count);
        }
    }
}
